package braipetro.interfaces.regras;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */



import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Enumeration;
import braipetro.data_access.DataConnection;
import braipetro.data_access.DataSet;
import braipetro.data_access.DataFieldType;



/**
 *
 * @author manasses
 */
public class ModRegras extends UnicastRemoteObject implements IRegras {

    private Dictionary<String, Regra> regras = new Hashtable<>();
    private boolean started = false;
    
    public ModRegras() throws RemoteException {
        super();
        initRegras();
    }
    
    private void initRegras() {
        
        DataConnection cnn = new DataConnection();
        cnn.loadFromFile("conn.conf");
        cnn.open();
        
        DataSet dtsRegra = new DataSet(cnn);
        dtsRegra.setSql("select id_regra, nome, formula, descricao from regra");
        
        DataSet dtsCriterio = new DataSet(cnn, dtsRegra);
        dtsCriterio.setSql(""
                + "select rc.id_criterio, rc.nome as nome_criterio, rc.valor_referencia, rc.tempo_atualizacao, rc.operador, "
                + "mt.nome as nome_metodo, mt.classe, mt.pacote, mt.tipo_retorno " 
                + "from regra_criterio rc "
                + "inner join metodo mt on rc.id_metodo = mt.id_metodo "
                + "where rc.id_regra = :id_regra"
        );
        
        DataSet dtsAcao = new DataSet(cnn, dtsRegra);
        dtsAcao.setSql(""
                + "select ra.id_regra_acoes, "
                + "mt.nome, mt.classe, mt.pacote "
                + "from regra_acao ra "
                + "inner join metodo mt on ra.id_metodo = mt.id_metodo "
                + "where ra.id_regra = :id_regra"
        );
        
        dtsRegra.open();
        
        while (dtsRegra.next()) {
            
            Regra rg = new Regra();
            rg.setNome(dtsRegra.getValue("nome"));
            rg.setFormula(dtsRegra.getValue("formula"));
            
            while (dtsCriterio.next()) {
                Metodo mtd = new Metodo(dtsCriterio.getValue("pacote"), dtsCriterio.getValue("classe"), dtsCriterio.getValue("nome_metodo"));
                
                Criterio cri = new Criterio();
                cri.setMetodo(mtd);
                cri.setNome(dtsCriterio.getValue("nome_criterio"));
                cri.setValorRef(Double.valueOf(dtsCriterio.getValue("valor_referencia")));
                cri.setTempoAtualizacao(Long.valueOf(dtsCriterio.getValue("tempo_atualizacao")));
                cri.setOperador(dtsCriterio.getValue("operador"));
                
                rg.addCriterio(cri);
                
            }

            while (dtsAcao.next()) {
                Metodo acao = new Metodo(dtsAcao.getValue("pacote"), dtsAcao.getValue("classe"), dtsAcao.getValue("nome"));
                
                rg.addAction(acao);
            }

            regras.put(rg.getNome(), rg); 
            
        }
        
        cnn.close();
    }
    
    @Override
    public boolean start() throws RemoteException {
        Enumeration enu = regras.keys();
        
        while(enu.hasMoreElements()) {
            regras.get(enu.nextElement()).start();
        }
        
        
        this.started = true;
        return true;
    }

    @Override
    public boolean stop() throws RemoteException {
        Enumeration enu = regras.keys();
        
        while(enu.hasMoreElements()) {
            regras.get(enu.nextElement()).stop();
        }
        
        this.started = false;
        return true;
    }

    @Override
    public boolean isStarted() throws RemoteException {
        return this.started;
    }
    
    @Override
    public boolean restart() throws RemoteException {
       Enumeration key = regras.keys();
        
        while(key.hasMoreElements()) {
            regras.get(key).restart();
        }
        
        this.started = true;
        return true;
    }

    @Override
    public boolean startRegra(String rg) throws RemoteException {
        
        if (!regras.get(rg).getStatus())
            regras.get(rg).start();
        
        return true;
    }

    @Override
    public boolean stopRegra(String rg) throws RemoteException {
         if (regras.get(rg).getStatus())
            regras.get(rg).stop();
        
        return true;
    }

    @Override
    public boolean isTriggeredRegra(String rg) throws RemoteException {        
        return regras.get(rg).isTriggered();
    }

    @Override
    public boolean isStartedRegra(String rg) throws RemoteException {
        return regras.get(rg).isStarted();
    }

    @Override
    public int getNumRegras() throws RemoteException {
        return regras.size();
    }

    @Override
    public String getRegras() throws RemoteException {
        String res = "";
        
       
        
        return res;
    }
    
}
